package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.ResponseResult;
import com.example.demo.entity.Stock;
import com.example.demo.mapper.StockMapper;
import com.example.demo.service.StockService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {
    /**
     * 查询全部库存信息
     * @return
     */
    @Override
    public ResponseResult getList() {
        List<Stock> stocks = list();

        if(stocks.isEmpty()){
            return ResponseResult.ok("没有库存信息");
        }
        return ResponseResult.ok(stocks);
    }

    /**
     * 保存新的库存信息
     * @param stock
     * @return
     */
    @Override
    public ResponseResult saveStock(Stock stock) {
        if(stock == null){
            return ResponseResult.fail("请输入正确的库存信息");
        }
        boolean isSuccess = save(stock);
        if(!isSuccess){
            return ResponseResult.fail("新增失败");
        }
        return ResponseResult.ok("新增成功");
    }

    /**
     * 更新库存信息
     * @param stock
     * @return
     */
    @Override
    public ResponseResult updateStock(Stock stock) {
        if(stock == null){
            return ResponseResult.fail("请输入正确的库存信息");
        }
        boolean isSuccess = update(stock, new QueryWrapper<Stock>().eq("Stock_id", stock.getId()));

        if(!isSuccess){
            return ResponseResult.fail("更新失败");
        }
        return ResponseResult.ok("更新成功");
    }

    /**
     * 根据id删除库存信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(String id) {
        boolean isSuccess = remove(new QueryWrapper<Stock>().eq("Stock_id", id));

        if(!isSuccess){
            return ResponseResult.fail("删除失败");
        }

        return ResponseResult.ok("删除成功");
    }


}
